<?php
// +----------------------------------------------------------------------
// | Author: derek <693472824@qq.com> <http://www.bloveambition.com/>
// +----------------------------------------------------------------------

namespace Blovex4\Logic;

/**
 * 站点业务逻辑层
 *
 * @author derek <693472824@qq.com>
 */
class SiteLogic extends BaseLogic {

    /**
     * 根据站点ID获取站点
     * @param   integer $roid 站点ID
     * @author  derek <693472824@qq.com>
     */
    public function getSiteByID ($siteID=0) {
        
        if(empty($siteID) || !is_numeric($siteID)){
            return null;
        }
        
        $siteModel = M('Site');
        $map["site_id"] = (string)$siteID;
        $site = $siteModel->where($map)->find();
        return $site;
    }
    
    /**
     * 保存站点信息
     *
     * @author derek <693472824@qq.com>
     */
    public function saveAction() {
        
        $siteID = I('site_id');
        $siteModel = D('Site');
    
        if ($siteModel->create()) {
    
            if (empty($siteID)) {
                return $siteModel->add();
            } else {
                $map['site_id'] = $siteID;
                return $siteModel->where($map)->save();
            }
        } else {
            $this->error = $siteModel->getError();
            return false;
        }
    }

    /**
     * 站点删除
     * @param   integer|array 需要删除的站点ID
     * @return  boolean
     * @author  derek <693472824@qq.com>
     */
    public function del ($siteID) {
        
        if(empty($siteID)){
            return false;
        }
        
        if(is_array($siteID)){
            
            $map['site_id'] = array('in', $siteID);
        }elseif (is_numeric($siteID)){
            
            $map['site_id'] = trim((string)$siteID, ',');
        }
        
        $result = M('Site')->where($map)->delete();
        
        return $result;
    }
}